﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

    public class AdminSubject
    {
        public static bool InsertAdmin(Admin admin)
        {
            ClassDatabase db = new ClassDatabase();
            try
            {
                bool rs = false;
                string sql = "insert into admin(Username,Password,HashKey,Mobile,Email,IsActive) values(@username,@password,@hashkey,@mobile,@email,@isactive)";
                SqlParameter[] pa = new SqlParameter[6];
                pa[0] = new SqlParameter("@username", admin.Username);
                pa[1] = new SqlParameter("@password", admin.Password);
                pa[2] = new SqlParameter("@hashkey", admin.Hashkey);
                pa[3] = new SqlParameter("@mobile", admin.Mobile);
                pa[4] = new SqlParameter("@email", admin.Email);
                pa[5] = new SqlParameter("@isactive", admin.IsActive);
                SqlCommand command = db.GetCommand(sql, pa, false);
                int row = command.ExecuteNonQuery();
                if (row > 0)
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                db.CloseData();
            }
        }
        public static List<Admin> GetListManagerProfile()
        {
            ClassDatabase db = new ClassDatabase();
            try
            {
                List<Admin> list = new List<Admin>();
                string sql = "select *from admin";

                SqlCommand command = db.GetCommandNonParameter(sql, false);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Admin profile = new Admin();
                    profile.AdminID = int.Parse(reader["AdminID"].ToString());
                    profile.Username = reader["UserName"].ToString();
                    if (!string.IsNullOrEmpty(reader["Mobile"].ToString()))
                    {
                        profile.Mobile = reader["Mobile"].ToString();
                    }
                    if (!string.IsNullOrEmpty(reader["Email"].ToString()))
                    {
                        profile.Email = reader["Email"].ToString();
                    }
                    profile.IsActive = int.Parse(reader["IsActive"].ToString());

                    list.Add(profile);
                }
                return list;
            }
            catch
            {
                return null;
            }
            finally
            {
                db.CloseData();
            }
        }

        public static Admin GerManagerAccountDetail(string username)
        {
            ClassDatabase db = new ClassDatabase();
            try
            {

                Admin admin = null;
                string sql = "select * from admin where UserName=@userName";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@userName", username);

                SqlCommand command = db.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    admin = new Admin();
                    admin.AdminID = int.Parse(reader["AdminID"].ToString());
                    admin.Username = reader["UserName"].ToString();
                    admin.Password = reader["Password"].ToString();
                    admin.Hashkey = reader["HashKey"].ToString();
                    admin.IsActive = int.Parse(reader["IsActive"].ToString());
                }
                return admin;
            }
            catch (Exception ex)
            {

                return null;
            }
            finally
            {
                db.CloseData();
            }
        }

        public static Admin GerManagerAccountDetail(int managerID)
        {
            ClassDatabase db = new ClassDatabase();
            try
            {

                Admin account = null;
                string sql = "select * from admin where AdminID=@adminID";
                SqlParameter[] pa = new SqlParameter[1];
                pa[0] = new SqlParameter("@adminID", managerID);

                SqlCommand command = db.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    account = new Admin();
                    account.AdminID = int.Parse(reader["AdminID"].ToString());
                    account.Username = reader["UserName"].ToString();
                    account.Password = reader["Password"].ToString();
                    account.Hashkey = reader["HashKey"].ToString();
                    account.IsActive = int.Parse(reader["IsActive"].ToString());
                }
                return account;
            }
            catch (Exception ex)
            {
                //Utility.WriteLog("Exception get managerdetail : " + ex.Message);
                return null;
            }
            finally
            {
                db.CloseData();
            }
        }

        public static bool LoginManagerAccount(string username, string password)
        {
            ClassDatabase db = new ClassDatabase();
            try
            {
                bool rs = false;
                string sql = "select UserName,Password from admin where UserName=@userName and Password=@pass and IsActive=1";
                SqlParameter[] pa = new SqlParameter[2];
                pa[0] = new SqlParameter("@userName", username);
                pa[1] = new SqlParameter("@pass", password);

                SqlCommand command = db.GetCommand(sql, pa, false);
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    rs = true;
                }
                return rs;
            }
            catch (Exception ex)
            {

                return false;
            }
            finally
            {
                db.CloseData();
            }
        }
    }

